<template>
    <div class="p-2">
        <!-- 表头布局区域 -->
        <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
            :leave-active-class="proxy?.animate.searchAnimate.leave">
            <div v-show="showSearch" class="mb-[10px]">
                <el-card shadow="hover">
                    <el-form ref="queryFormRef" :model="queryParams" :inline="true">
                        <el-form-item label="区域名称" prop="name">
                            <el-input v-model="queryParams.name" placeholder="请输入区域名称" clearable
                                @keyup.enter="handleQuery" />
                        </el-form-item>
                        <el-form-item label="是否热门" prop="isHot">
                            <el-select v-model="queryParams.isHot" placeholder="请选择是否热门" clearable>
                                <el-option v-for="dict in sys_yes_no" :key="dict.value" :label="dict.label"
                                    :value="dict.value" />
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </div>
        </transition>

        <el-card shadow="never">
            <!-- 表格按钮区域 -->
            <template #header>
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="Plus" @click="handleAdd"
                            v-hasPermi="['system:cityRegion:add']">新增
                        </el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()"
                            v-hasPermi="['system:cityRegion:edit']">修改
                        </el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"
                            v-hasPermi="['system:cityRegion:remove']">删除
                        </el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="warning" plain icon="Download" @click="handleExport"
                            v-hasPermi="['system:cityRegion:export']">导出
                        </el-button>
                    </el-col>
                    <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>
            </template>

            <!-- 表格数据 -->
            <el-table v-loading="loading" border :data="cityRegionList" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="主键id" :show-overflow-tooltip="true" align="center" prop="id" v-if="false" />
                <el-table-column label="区域名称" :show-overflow-tooltip="true" align="center" prop="name" />
                <el-table-column label="区域上级标识" :show-overflow-tooltip="true" align="center" prop="pid" />
                <el-table-column label="地名简称" :show-overflow-tooltip="true" align="center" prop="shortName" />
                <el-table-column label="区域等级" :show-overflow-tooltip="true" align="center" prop="level" />
                <el-table-column label="区域编码" :show-overflow-tooltip="true" align="center" prop="cityCode" />
                <el-table-column label="邮政编码" :show-overflow-tooltip="true" align="center" prop="postalCode" />
                <el-table-column label="组合名称" :show-overflow-tooltip="true" align="center" prop="merName" />
                <el-table-column label="是否热门" :show-overflow-tooltip="true" align="center" prop="isHot">
                    <template #default="scope">
                        <dict-tag :options="sys_yes_no" :value="scope.row.isHot" />
                    </template>
                </el-table-column>
                <el-table-column label="备注" :show-overflow-tooltip="true" align="center" prop="remark" />
                <el-table-column label="操作" width="150" align="center" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <el-tooltip content="修改" placement="top">
                            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                                v-hasPermi="['system:cityRegion:edit']"></el-button>
                        </el-tooltip>
                        <el-tooltip content="删除" placement="top">
                            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                                v-hasPermi="['system:cityRegion:remove']"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 列表分页组件 -->
            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </el-card>

        <!-- 添加或修改地址信息对话框 -->
        <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
            <el-form ref="cityRegionFormRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="区域名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入区域名称" />
                </el-form-item>
                <el-form-item label="区域上级标识" prop="pid">
                    <el-input v-model="form.pid" placeholder="请输入区域上级标识" />
                </el-form-item>
                <el-form-item label="地名简称" prop="shortName">
                    <el-input v-model="form.shortName" placeholder="请输入地名简称" />
                </el-form-item>
                <el-form-item label="区域等级" prop="level">
                    <el-input v-model="form.level" placeholder="请输入区域等级" />
                </el-form-item>
                <el-form-item label="区域编码" prop="cityCode">
                    <el-input v-model="form.cityCode" placeholder="请输入区域编码" />
                </el-form-item>
                <el-form-item label="邮政编码" prop="postalCode">
                    <el-input v-model="form.postalCode" placeholder="请输入邮政编码" />
                </el-form-item>
                <el-form-item label="组合名称" prop="merName">
                    <el-input v-model="form.merName" placeholder="请输入组合名称" />
                </el-form-item>
                <el-form-item label="是否热门" prop="isHot">
                    <el-select v-model="form.isHot" placeholder="请选择是否热门">
                        <el-option v-for="dict in sys_yes_no" :key="dict.value" :label="dict.label"
                            :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="CityRegion" lang="ts">
import { listCityRegion, getCityRegion, delCityRegion, addCityRegion, updateCityRegion } from '@/api/system/cityRegion/index';
import { CityRegionVO, CityRegionQuery, CityRegionForm } from '@/api/system/cityRegion/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_yes_no } = toRefs<any>(proxy?.useDict('sys_yes_no'));

const cityRegionList = ref<CityRegionVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const cityRegionFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
    visible: false,
    title: ''
});

const initFormData: CityRegionForm = {
    id: undefined,
    name: undefined,
    pid: undefined,
    shortName: undefined,
    level: undefined,
    cityCode: undefined,
    postalCode: undefined,
    merName: undefined,
    isHot: undefined,
    remark: undefined,
}
const data = reactive<PageData<CityRegionForm, CityRegionQuery>>({
    form: { ...initFormData },
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        isHot: undefined,
        params: {
        }
    },
    rules: {
        name: [{ required: true, message: "区域名称不能为空", trigger: "blur" }],
        shortName: [{ required: true, message: "地名简称不能为空", trigger: "blur" }],
        level: [{ required: true, message: "区域等级不能为空", trigger: "blur" }],
        cityCode: [{ required: true, message: "区域编码不能为空", trigger: "blur" }],
        postalCode: [{ required: true, message: "邮政编码不能为空", trigger: "blur" }],
        merName: [{ required: true, message: "组合名称不能为空", trigger: "blur" }],
        isHot: [{ required: true, message: "是否热门不能为空", trigger: "change" }],
    }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询地址信息列表 */
const getList = async () => {
    loading.value = true;
    const res = await listCityRegion(queryParams.value);
    cityRegionList.value = res.rows;
    total.value = res.total;
    loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
    reset();
    dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
    form.value = { ...initFormData };
    cityRegionFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
    queryFormRef.value?.resetFields();
    handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: CityRegionVO[]) => {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
    reset();
    dialog.visible = true;
    dialog.title = "添加地址信息";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: CityRegionVO) => {
    reset();
    const _id = row?.id || ids.value[0]
    const res = await getCityRegion(_id);
    Object.assign(form.value, res.data);
    dialog.visible = true;
    dialog.title = "修改地址信息";
}

/**
 * 提交按钮操作
 */
const submitForm = () => {
    cityRegionFormRef.value?.validate(async (valid: boolean) => {
        if (valid) {
            buttonLoading.value = true;
            if (form.value.id) {
                await updateCityRegion(form.value).finally(() => buttonLoading.value = false);
            } else {
                await addCityRegion(form.value).finally(() => buttonLoading.value = false);
            }
            proxy?.$modal.msgSuccess('操作成功');
            dialog.visible = false;
            await getList();
        }
    });
}

/**
 * 删除按钮操作
 * 
 * @param row 单行数据
 */
const handleDelete = async (row?: CityRegionVO) => {
    const _ids = row?.id || ids.value;
    await proxy?.$modal.confirm('是否确认删除地址信息编号为"' + _ids + '"的数据项?');
    await delCityRegion(_ids);
    await getList();
    proxy?.$modal.msgSuccess('删除成功');
}

/**
 * 导出按钮操作
 */
const handleExport = () => {
    proxy?.download('system/cityRegion/export', {
        ...queryParams.value
    }, `cityRegion_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
    getList();
});
</script>
